package com.wangsh.userscenter.outer.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.userscenter.common.service.BaseServiceImpl;
import com.wangsh.userscenter.common.util.ConstatFinalUtil;
import com.wangsh.userscenter.outer.service.IOuterService;
import com.wangsh.userscenter.system.pojo.ARegion;
import com.wangsh.userscenter.system.service.ISystemService;

/**
 * 接口的实现类
 * @author WangshSxt
 *
 */
@Service("outerService")
public class OuterServiceImpl extends BaseServiceImpl implements IOuterService
{
	@Resource
	private ISystemService systemService ; 
	
	/**
	 * 验证加密的字符串
	 * 
	 * 加密参数;sha256(version+pubKey+私钥) 
	 * 
	 * @return
	 */
	private JSONObject verifyEncryptStr(JSONObject reqJSON)
	{
		JSONObject resultJSON = new JSONObject() ; 
		String version = reqJSON.getString("version");
		String pubKey = reqJSON.getString("pubKey");
		String encryptStr = reqJSON.getString("encryptStr");
		
		/* 为了安全,私钥不能从请求中获取,读取本地的配置文件
		 * 私钥:客户端和服务器端同时存储到配置文件中
		 *  */
		String priKey = ConstatFinalUtil.CONFIG_JSON.getString("private.key");
		
		String encHou = DigestUtils.sha256Hex(version + pubKey + priKey);
		if(encHou.equalsIgnoreCase(encryptStr) || "0".equalsIgnoreCase(ConstatFinalUtil.CONFIG_JSON.getString("version")))
		{
			/* 成功 */
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0") + "");
		}else
		{
			/* 请求非法,您在恶意攻击 */
			resultJSON.put("code", "14");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("14") + "");
		}
		return resultJSON ; 
	}
	
	@Override
	public JSONObject verfiyUsersToken01Service(JSONObject reqJSON)
	{
		/* 返回的最终结果 */
		JSONObject resultJSON = new JSONObject() ; 
		
		JSONObject dataReqJSON = (JSONObject) reqJSON.get("data");
		String token = dataReqJSON.get("token") + "";
		
		String key = "users_token_" + token; 
		String json = this.redisUtil.get(key) + "";
		JSONObject usersJSON = (JSONObject) JSON.parse(json);
		if(usersJSON != null)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("users", usersJSON);
			
			/* 重新再放一次 */
			this.redisUtil.put(key, json, Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("timeout.token")));
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "5");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("5"));
		}
		return resultJSON ; 
	}

	@Override
	public JSONObject regionList01Service(JSONObject reqJSON)
	{
		JSONObject resultJSON = new JSONObject();
		JSONObject dataResJSON = new JSONObject(); 
		
		/* 解析上行信息 */
		JSONObject dataReqJSON = reqJSON.getJSONObject("data");
		
		String parentId = dataReqJSON.getString("parentId");
		/* 查询数据 */
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("parentId", parentId);
		List<ARegion> regionList = this.systemService.findCondListRegionService(null, condMap);
		JSONArray regionArr = new JSONArray();
		for (Iterator<ARegion> iterator = regionList.iterator(); iterator.hasNext();)
		{
			ARegion region = (ARegion) iterator.next();
			regionArr.add(region.toJSON());
		}
		
		dataResJSON.put("regionList", regionArr);
		resultJSON.put("data", dataResJSON);
		resultJSON.put("code", "0");
		resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		return resultJSON;
	}

	@Override
	public JSONObject verfiyAdminsToken01Service(JSONObject reqJSON)
	{
		/* 返回的最终结果 */
		JSONObject resultJSON = new JSONObject() ; 
		
		JSONObject dataReqJSON = (JSONObject) reqJSON.get("data");
		String token = dataReqJSON.get("token") + "";
		
		String key = "admins_token_" + token; 
		String json = this.redisUtil.get(key) + "";
		JSONObject adminsJSON = (JSONObject) JSON.parse(json);
		if(adminsJSON != null)
		{
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("admins", adminsJSON);
			
			/* 重新再放一次 */
			this.redisUtil.put(key, json, Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.getString("timeout.token")));
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultJSON.put("code", "5");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("5"));
		}
		return resultJSON ; 
	}
}
